package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.StorageGatherDetailsDto;
import com.guigu.erp.dto.StorageGatherDto;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.pojo.StorageGather;
import com.guigu.erp.pojo.StorageGatherDetails;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.service.StorageGatherDetailsService;
import com.guigu.erp.service.StorageGatherService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.StorageGatherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@RequestMapping("/storage/gather")
@Controller
public class StorageGatherController {
    @Autowired
    private StorageGatherService storageGatherService;

    @Autowired
    private StorageGatherDetailsService storageGatherDetailsService;

    @Autowired
    private StorageCellService storageCellService;

    @Autowired
    private NumberUtils numberUtils;

    //添加入库申请单和入库明细单
    @RequestMapping("addInbound")
    @ResponseBody
    public boolean addInbound(@RequestBody StorageGatherDto dto){
        String number = numberUtils.getTypeNumber("401");
        //设置入库编号
        dto.setGatherId(number);
        //设置审核标志
        dto.setCheckTag("S001-0");
       //设置库存标志
        dto.setStoreTag("K002-1");
        //设置确认入库总件数
        dto.setGatheredAmountSum(new BigDecimal(0));
        boolean flag = storageGatherService.addInboundWithDetails(dto);
        return flag;
    }

    //查询未审核的入库申请单
    @RequestMapping("queryNotReviewed")
    @ResponseBody
    public PageUtils<StorageGather> queryNotReviewed(Integer pageNo,Integer pageSize){
        QueryWrapper<StorageGather> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG","S001-0");
        PageHelper.startPage(pageNo,pageSize);
        List<StorageGather> list = storageGatherService.list(wrapper);
        PageInfo<StorageGather> pageInfo = new PageInfo<>(list);
        PageUtils<StorageGather> pageUtils = new PageUtils<>();
        pageUtils.setTotal(pageInfo.getTotal());
        pageUtils.setRows(pageInfo.getList());
        return pageUtils;
    }

    //根据id查询入库单
    @RequestMapping("getByIdStorage")
    @ResponseBody
    public StorageGather getByIdStorage(Integer id){
        StorageGather storageGather = storageGatherService.getById(id);
        return storageGather;
    }

    //根据id查询入库明细
    @RequestMapping("getIdStorageDetails")
    @ResponseBody
    public List<StorageGatherDetails> getIdStorageDetails(Integer id){
        QueryWrapper<StorageGatherDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",id);
        List<StorageGatherDetails> details = storageGatherDetailsService.list(wrapper);
        return details;
    }

    //点击修改入库审核标志
    @RequestMapping("updStorage")
    @ResponseBody
    public boolean updStorage(StorageGather storageGather){
        String reason = storageGather.getReason();
        if(reason.equals("生产入库")){
            storageGather.setReason("R001-1");
        }else if (reason.equals("库存初始")){
            storageGather.setReason("R001-2");
        }else if (reason.equals("赠送")){
            storageGather.setReason("R001-3");
        }else if (reason.equals("内部归还")){
            storageGather.setReason("R001-4");
        }else{
            storageGather.setReason("R001-5");
        }
        boolean flag = storageGatherService.updateById(storageGather);
        return flag;
    }

    //查询所有的入库设计单
    @RequestMapping("getAllStorageGather")
    @ResponseBody
    public PageUtils<StorageGather> getAllStorageGather(StorageGatherVo vo,Integer pageNo,Integer pageSize){
        PageUtils<StorageGather> pageUtils = storageGatherService.getAllStorage(vo, pageNo, pageSize);
        return pageUtils;
    }

    //查询制定了入库设计单
    @RequestMapping("queryStorageAuditPass")
    @ResponseBody
    public PageUtils<StorageGather> queryStorageAuditPass(StorageGatherVo vo,Integer pageNo,Integer pageSize){
        PageUtils<StorageGather> pageUtils = storageGatherService.getStorageAuditPass(vo, pageNo, pageSize);
        return pageUtils;
    }

    //根据id查询入库设计单表
    @RequestMapping("getByIdStorageGather")
    @ResponseBody
    public StorageGather getByIdStorageGather(Integer id){
        StorageGather storageGather = storageGatherService.getById(id);
        return storageGather;
    }

    //根据id查询入库明细设计单表
    @RequestMapping("getByIdStorageGatherDetails")
    @ResponseBody
    public List<StorageGatherDetails> getByIdStorageGatherDetails(Integer id){
        QueryWrapper<StorageGatherDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",id);
        List<StorageGatherDetails> list = storageGatherDetailsService.list(wrapper);
        return list;
    }

    //根据id判断是否制定安全配置单
    @RequestMapping("getByProductIdMake")
    @ResponseBody
    public boolean getByProductIdMake(Integer id){
        StorageGatherDetails byId = storageGatherDetailsService.getById(id);
        QueryWrapper<StorageCell> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",byId.getProductId());
        StorageCell one = storageCellService.getOne(wrapper);
        if(one==null){
            return false;
        }
        return one.getCheckTag().equals("S001-1");
    }

    //根据id查询入库明细
    @RequestMapping("getIdQueryStorageDetails")
    @ResponseBody
    public StorageGatherDetails getIdQueryStorageDetails(Integer id){
        StorageGatherDetails details = storageGatherDetailsService.getById(id);
        return details;
    }

    //修改入库明细 入库
    @RequestMapping("updGatherWithDetail")
    @ResponseBody
    public boolean updGatherWithDetail(StorageGatherDetailsDto dto){
        boolean flag = storageGatherDetailsService.updStorageGatherWithDetail(dto);
        return flag;
    }

    @RequestMapping("getGatherId")
    @ResponseBody
    public Boolean getGatherId(String gatherId){
        QueryWrapper<StorageGather> wrapper = new QueryWrapper<>();
        wrapper.eq("GATHER_ID",gatherId);
        StorageGather one = storageGatherService.getOne(wrapper);
        return one.getStoreTag().equals("K002-2");
    }

}